﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GISCommon.Tools
{
    public class Drawing
    {
        #region DrawPoint
        public static void DrawPoint(ESRI.ArcGIS.Controls.AxMapControl mapControl,
            ESRI.ArcGIS.Geometry.IPoint point)
        {
            try
            {
                ESRI.ArcGIS.Carto.IActiveView activeView = mapControl.ActiveView.FocusMap as
                    ESRI.ArcGIS.Carto.IActiveView;
                if (activeView == null) return;

                ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

                screenDisplay.StartDrawing(screenDisplay.hDC,
                    (short)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);
                screenDisplay.SetSymbol(
                    new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass());
                screenDisplay.DrawPoint(point);
                screenDisplay.FinishDrawing();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPoint出错", ex);
            }
        }
        public static void DrawPoint(ESRI.ArcGIS.Controls.AxMapControl mapControl,
            ESRI.ArcGIS.Geometry.IPoint point,int Red,int Green,int Blue,int Size)
        {
            try
            {
                ESRI.ArcGIS.Carto.IActiveView activeView = mapControl.ActiveView.FocusMap as
                    ESRI.ArcGIS.Carto.IActiveView;
                if (activeView == null) return;

                ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

                screenDisplay.StartDrawing(screenDisplay.hDC,
                    (short)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);
                
                ESRI.ArcGIS.Display.IMarkerSymbol msym= new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
                ESRI.ArcGIS.Display.IRgbColor color = new ESRI.ArcGIS.Display.RgbColorClass();
                color.Red = Red;
                color.Green = Green;
                color.Blue = Blue;
                msym.Color = color;
                msym.Size = Size;

                ESRI.ArcGIS.Display.ISymbol sym = (ESRI.ArcGIS.Display.ISymbol)msym;

                screenDisplay.SetSymbol(
                    sym);
                screenDisplay.DrawPoint(point);
                screenDisplay.FinishDrawing();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPoint出错", ex);
            }
        }
        public static void DrawText(ESRI.ArcGIS.Controls.AxMapControl mapControl,
            ESRI.ArcGIS.Geometry.IPoint point,string txt,int fontSize)
        {
            try
            {
                //ESRI.ArcGIS.Carto.IActiveView activeView = mapControl.ActiveView.FocusMap as
                //    ESRI.ArcGIS.Carto.IActiveView;
                //if (activeView == null) return;

                //ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

                //screenDisplay.StartDrawing(screenDisplay.hDC,
                //    (short)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);

                ////screenDisplay.SetSymbol(
                ////    sym);
                //screenDisplay.DrawText(point, txt);
                //screenDisplay.FinishDrawing();
                int x;
                int y;
                mapControl.ActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(
                    point,out x,out y);
                using (System.Drawing.Graphics g = mapControl.CreateGraphics())
                {
                    g.DrawString(txt,
                        new System.Drawing.Font("宋体", fontSize), System.Drawing.Brushes.Red,
                        new System.Drawing.PointF(x, y-fontSize-3));
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPoint出错", ex);
            }
        }
        #endregion

        #region DrawPolyline
        //static ESRI.ArcGIS.Display.IRubberBand m_rubberLine =
        //        new ESRI.ArcGIS.Display.RubberLineClass();
        static ESRI.ArcGIS.Display.ISimpleLineSymbol m_lineSymbol = null;
        static ESRI.ArcGIS.Geometry.IPolyline m_polyline = null;
        static ESRI.ArcGIS.Controls.AxMapControl m_mapControl = null;

        public static void DrawPolyline(ESRI.ArcGIS.Controls.AxMapControl mapControl)
        {
            try
            {
                ESRI.ArcGIS.Carto.IActiveView activeView = mapControl.ActiveView.FocusMap as
                    ESRI.ArcGIS.Carto.IActiveView;
                if (activeView == null) return;

                ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

                ESRI.ArcGIS.Display.ISimpleLineSymbol lineSymbol =
                    new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
                ESRI.ArcGIS.Display.IRgbColor rgbColor =
                    new ESRI.ArcGIS.Display.RgbColorClass();
                rgbColor.Red = 255;
                lineSymbol.Color = rgbColor;
                ESRI.ArcGIS.Display.IRubberBand rubberLine =
                    new ESRI.ArcGIS.Display.RubberLineClass();

                ESRI.ArcGIS.Geometry.IPolyline newPolyline =
                    (ESRI.ArcGIS.Geometry.IPolyline)rubberLine.TrackNew(
                    screenDisplay, (ESRI.ArcGIS.Display.ISymbol)lineSymbol);

                screenDisplay.StartDrawing(screenDisplay.hDC,
                    (short)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);

                screenDisplay.SetSymbol((ESRI.ArcGIS.Display.ISymbol)lineSymbol);

                m_lineSymbol = lineSymbol;
                m_polyline = newPolyline;
                m_mapControl = mapControl;

                screenDisplay.DrawPolyline(newPolyline);
                screenDisplay.FinishDrawing();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPolyline(ESRI.ArcGIS.Controls.AxMapControl mapControl)出错", ex);
            }
        }
        public static void ExitDrawPolyline()
        {
            try
            {
                if (m_lineSymbol == null || m_polyline == null || m_mapControl == null)
                    return;
                ESRI.ArcGIS.Display.IRubberBand rubberLine =
                    new ESRI.ArcGIS.Display.RubberLineClass();
                rubberLine.TrackExisting(m_mapControl.ActiveView.ScreenDisplay, 
                    (ESRI.ArcGIS.Display.ISymbol)m_lineSymbol, 
                    (ESRI.ArcGIS.Geometry.IGeometry)m_polyline);
                m_polyline = null;
                m_lineSymbol = null; 
                m_mapControl = null;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ExitDrawPolyline()出错", ex);
            }
        }

        public static void ExitDrawPolyline(ESRI.ArcGIS.Controls.AxMapControl mapControl, 
            ESRI.ArcGIS.Geometry.IGeometry polyline)
        {
            try
            {
                if (polyline == null)
                    return;
                mapControl.ActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGraphics, 
                    polyline, polyline.Envelope);
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ExitDrawPolyline()出错", ex);
            }
        }

        public static void DrawPolyline(ESRI.ArcGIS.Controls.AxMapControl mapControl,
            ESRI.ArcGIS.Geometry.IGeometry polyline)
        {
            try
            {
                ESRI.ArcGIS.Carto.IActiveView activeView = mapControl.ActiveView.FocusMap as
                    ESRI.ArcGIS.Carto.IActiveView;
                if (activeView == null) return;

                ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

                ESRI.ArcGIS.Display.ISimpleLineSymbol lineSymbol =
                    new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
                ESRI.ArcGIS.Display.IRgbColor rgbColor =
                    new ESRI.ArcGIS.Display.RgbColorClass();
                rgbColor.Red = 255;
                lineSymbol.Color = rgbColor;

                screenDisplay.StartDrawing(screenDisplay.hDC,
                    (short)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);

                screenDisplay.SetSymbol((ESRI.ArcGIS.Display.ISymbol)lineSymbol);

                screenDisplay.DrawPolyline(polyline);
                screenDisplay.FinishDrawing();

                //return (ESRI.ArcGIS.Display.ISymbol)lineSymbol;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPolyline出错", ex);
            }
        }
        public static void DrawPolyline(ESRI.ArcGIS.Controls.AxMapControl mapControl,
    ESRI.ArcGIS.Geometry.IGeometry polyline,int Red,int Green,int Blue,int width)
        {
            try
            {
                ESRI.ArcGIS.Carto.IActiveView activeView = mapControl.ActiveView.FocusMap as
                    ESRI.ArcGIS.Carto.IActiveView;
                if (activeView == null) return;

                ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

                ESRI.ArcGIS.Display.ISimpleLineSymbol lineSymbol =
                    new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
                ESRI.ArcGIS.Display.IRgbColor rgbColor =
                    new ESRI.ArcGIS.Display.RgbColorClass();
                rgbColor.Red = Red;
                rgbColor.Green = Green;
                rgbColor.Blue = Blue;
                lineSymbol.Color = rgbColor;
                lineSymbol.Width = width;

                screenDisplay.StartDrawing(screenDisplay.hDC,
                    (short)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache);

                screenDisplay.SetSymbol((ESRI.ArcGIS.Display.ISymbol)lineSymbol);

                screenDisplay.DrawPolyline(polyline);
                screenDisplay.FinishDrawing();

                //return (ESRI.ArcGIS.Display.ISymbol)lineSymbol;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用DrawPolyline出错", ex);
            }
        }
        #endregion
    }
}
